const DataSource = require('../models/DataSource.js');
const { Op } = require('sequelize');

// 获取所有数据源（分页）
const getDataSources = async (ctx) => {
  try {
    const page = parseInt(ctx.query.page) || 1;
    const pageSize = parseInt(ctx.query.pageSize) || 10;
    const offset = (page - 1) * pageSize;
    const { description } = ctx.query;
    const { type } = ctx.query;

    const whereClause = {};
    if (description) {
      whereClause.description = {
        [Op.like]: `%${description}%`,
      };
    }

    if (type) {
      whereClause.type = {
        [Op.like]: `%${type}%`,
      };
    }



    const { count, rows } = await DataSource.findAndCountAll({
      where: whereClause,
      offset: offset,
      limit: pageSize,
    });

    ctx.body = {
      code: 200,
      data: {
        total: count,
        records: rows,
      },
      message: 'Success',
    };
  } catch (error) {
    ctx.status = 500;
    ctx.body = { code: 500, message: error.message };
  }
};

// 根据ID获取数据源
const getDataSourceById = async (ctx) => {
  try {
    const dataSource = await DataSource.findByPk(ctx.params.id);
    if (dataSource) {
      ctx.body = {
        code: 200,
        data: dataSource,
        message: 'Success',
      };
    } else {
      ctx.status = 404;
      ctx.body = { code: 404, message: 'Data source not found' };
    }
  } catch (error) {
    ctx.status = 500;
    ctx.body = { code: 500, message: error.message };
  }
};

// 创建新数据源
const createDataSource = async (ctx) => {
  try {
    const dataSource = await DataSource.create(ctx.request.body);
    ctx.status = 200;
    ctx.body = {
      code: 200,
      data: dataSource,
      message: 'Data source created successfully',
    };
  } catch (error) {
    ctx.status = 400;
    ctx.body = { code: 400, message: error.message };
  }
};

// 更新数据源
const updateDataSource = async (ctx) => {
  try {
    const [updated] = await DataSource.update(ctx.request.body, {
      where: { id: ctx.params.id },
    });
    if (updated) {
      const updatedDataSource = await DataSource.findByPk(ctx.params.id);
      ctx.body = {
        code: 200,
        data: updatedDataSource,
        message: 'Data source updated successfully',
      };
    } else {
      ctx.status = 404;
      ctx.body = { code: 404, message: 'Data source not found' };
    }
  } catch (error) {
    ctx.status = 400;
    ctx.body = { code: 400, message: error.message };
  }
};

// 删除数据源
const deleteDataSource = async (ctx) => {
  try {
    const deleted = await DataSource.destroy({
      where: { id: ctx.params.id },
    });
    if (deleted) {
        ctx.body = {
        code: 200,
        message: 'Data source deleted successfully',
      };
    } else {
      ctx.status = 404;
      ctx.body = { code: 404, message: 'Data source not found' };
    }
  } catch (error) {
    ctx.status = 500;
    ctx.body = { code: 500, message: error.message };
  }
};

module.exports = {
  getDataSources,
  getDataSourceById,
  createDataSource,
  updateDataSource,
  deleteDataSource
};